package com.yiqi.news.app.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.flyco.roundview.RoundTextView;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.util.DensityUtil;
import com.yiqi.news.R;
import com.yiqi.news.app.utils.DateUtils;
import com.yiqi.news.app.utils.TimeUtils;
import com.yiqi.news.app.widget.zwcalendar.ZWSignCalendarView;
import com.yiqi.news.entity.SignHistoryVO;

import java.util.HashSet;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SignDialog extends Dialog {


    @BindView(R.id.calendarView)
    ZWSignCalendarView calendarView;
    @BindView(R.id.btn_sign_ok)
    RoundTextView tvVideoLong;
    @BindView(R.id.tv_sign_count)
    TextView tvSignCount;
    @BindView(R.id.tv_show_mouth)
    TextView tvShowMouth;

    private Context context;

    private MyClickListenerInterface listenerInterface;
    private LayoutInflater layoutInflater;

    List<SignHistoryVO> mData;

    public SignDialog(Context context, List<SignHistoryVO> data) {
        super(context, R.style.ActionImmediatelyDialogStyle);
        this.context = context;
        this.mData = data;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        View view = layoutInflater.inflate(R.layout.dialog_sign, null);
        ButterKnife.bind(this, view);
        setContentView(view);

        if (mData != null && mData.size() > 0) {
            tvSignCount.setText(String.format("本月已累计签到%s天", mData.size()));
            if (DateUtils.getCurrentTime().equals(mData.get(mData.size() - 1).getToday())) {
                tvVideoLong.getDelegate().setBackgroundColor(Color.parseColor("#CCCCCC"));
                tvVideoLong.setEnabled(false);
                tvVideoLong.setText("已签到");
            } else {
                tvVideoLong.getDelegate().setBackgroundColor(ArmsUtils.getColor(context, R.color.color_f04d22));
                tvVideoLong.setText("签到");
                tvVideoLong.setEnabled(true);
            }

        }
        HashSet<String> sign2 = new HashSet<>();
        for (int i = 0; i < mData.size(); i++) {
            sign2.add(TimeUtils.stampToDate3(mData.get(i).getCreateTime()));
        }
        calendarView.setSignRecords(sign2);

        calendarView.setDateListener(new ZWSignCalendarView.DateListener() {
            @Override
            public void change(int year, int month) {
                tvShowMouth.setText(String.format("%s 年 %s 月", year, month));
            }
        });

        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;


        getWindow().getDecorView().setPadding(DensityUtil.dp2px(24), 0, DensityUtil.dp2px(24), 0);
        getWindow().setAttributes(layoutParams);

    }

    public void setListener(MyClickListenerInterface listener) {
        this.listenerInterface = listener;
    }

    @OnClick({R.id.btn_sign_close, R.id.btn_sign_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_sign_close:
                dismiss();
                break;
            case R.id.btn_sign_ok:
                listenerInterface.doConfirm();
                break;
        }
    }


    public interface MyClickListenerInterface {
        void doConfirm();
    }

}
